import fetch from './fetch'
import crypto from 'crypto'

const tokenService = {}

// 登录
/*
  @param = {
    "appid":"应用id",
    "timestamp":"时间戳",
    "sign":"签名"
  }
  POST /API/Token/AccessToken
 */
tokenService.AccessToken = function () {
  const params = {}
  // 应用id
  var appid = '1523960651'
  // 应用密钥
  var appsecret = 'ac90bb1eba4a4e94b7057da9e9f61b0d'
  // 加密常量
  var text = '[Lucifer]'
  // 时间戳
  var timestamp = Math.round(new Date().getTime() / 1000)
  // 加密规则
  var rule = appid + appsecret + text + timestamp + appid
  var md5 = crypto.createHash('md5')
  md5.update(rule)
  // 计算签名
  var sign = md5.digest('hex')
  console.log(sign)
  params.timestamp = timestamp
  params.sign = sign
  params.appid = appid
  return fetch({
    url: 'Token/AccessToken',
    method: 'post',
    data: params
  })
}

export default tokenService
